<template>
  <el-container class="home-container">
    <!--  头部区域  -->
    <el-header>
      <div>
        <img src="../assets/img/heima.png">
        <span>电商管理后台</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!--  页面主题区域  -->
    <el-container>
      <!--  侧边栏  -->
      <el-aside :width="isCollapse ? '60px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu background-color="#333744" text-color="#fff"
                 unique-opened :collapse="isCollapse" :collapse-transition="false" router
                 :default-active="activePath">
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item-group v-for="subItem in item.children"
                                :key="subItem.id" >
              <el-menu-item :index="'/' + subItem.path" @click="saveNavState('/' + subItem.path)">
                <i class="el-icon-menu"></i>
                {{subItem.authName}}
              </el-menu-item>
            </el-menu-item-group>

          </el-submenu>
        </el-menu>
      </el-aside>
      <!--  右侧栏主体  -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data(){
    return {
      //左侧菜单栏数据
      menulist : [],
      iconsObj:{
        '125':' iconfont icon-user',
        '103':' iconfont icon-tijikongjian',
        '101':' iconfont icon-shangpin',
        '102':' iconfont icon-danju',
        '145':' iconfont icon-baobiao'
      },
      //是否折叠
      isCollapse:false,
      //被激活的链接地址
      activePath:''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods:{
    logout(){
      window.sessionStorage.clear()//清除保存的token
      this.$router.push('/login')//跳转到login登陆界面
    },
    //获取所有的菜单
    async getMenuList(){
      const {data:res} = await this.$http.get("menus")
      if(res.meta.status != 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      console.log(res)
    },
    //点击切换菜单的折叠 展开
    toggleCollapse(){
      this.isCollapse = !this.isCollapse
    },
    //保存链接的激活方式
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style scoped>
  .home-container{
    height: 100%;
  }
  .toggle-button{
    background-color: #4A5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em; /*字符之间的间距*/
    cursor: pointer;/*鼠标放上去变成小手*/
  }
  .el-header{
    background-color: #373D41;
    display: flex;
    justify-content: space-between;/*两端*/
    padding-left: 0;
    align-items: center;/*上下不贴边*/
    color: #fff;
    font-size: 20px;
  }
  .el-header div {
    display: flex;
    align-items: center;
  }
  .el-header div span {
    margin-left: 15px;
  }
  .el-aside{
    background-color: #333744;
  }
  .el-main{
    background-color: #EAEDF1;
  }
  .el-menu{
    border: none;
  }
</style>